package cn.hinglo.common.plugin.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.quartz.Job;
import org.quartz.JobExecutionContext;

/**
 * @createTime: 2020/3/30 : 15:56
 * @author: HingLo
 * @description: mqtt 自动重连定时任务
 */
@Slf4j
public class MqttReConnectJob implements Job {
    @Override
    public void execute(JobExecutionContext context) {

        Boolean automaticReconnect = MqttManager.getSingleton().getAutomaticReconnect();
        if (automaticReconnect) {
            MqttClient client = MqttManager.getSingleton().getClient();
            if (!client.isConnected()) {
                try {
                    client.reconnect();
                } catch (MqttException e) {
                    log.error("MQTT 断开连接后，自动重连失败", e);
                }
            }
        }
    }
}
